<?php
function change_db($db)
{
	global $MySQL_Host, $MySQL_User, $MySQL_Password;
	//connection to server
	$mysql_link = mysql_connect($MySQL_Host, $MySQL_User, $MySQL_Password) or die ("Could not connect.");
	mysql_select_db($db, $mysql_link) or die (mysql_error());
}
function create_vote_lists($id,$db)
{
	change_db($db);
	$check_exist = "SELECT * FROM `voting_points` WHERE `id` LIKE '$id'";
	$check=mysql_query($check_exist) or die (mysql_error());
	if(!mysql_num_rows($check))
	{
		$create_list = "INSERT INTO `voting_points` (`id`) VALUES ('$id')";
		$create=mysql_query($create_list) or die (mysql_error());
	}
	$user_ip = getenv("REMOTE_ADDR");
	$check_exist = "SELECT * FROM `voting` WHERE `user_ip` LIKE '$user_ip'";
	$check=mysql_query($check_exist) or die (mysql_error());
	$row=mysql_fetch_array($check);
	$today=time();
	if(!mysql_num_rows($check))
	{
		$create_list = "INSERT INTO `voting` (`user_ip`) VALUES ('$user_ip')";
		$create=mysql_query($create_list) or die (mysql_error());
	}
	else 
		if(($today - $row['time']) > 43200)
		{
			$create_list = "UPDATE `voting` SET `sites`=0 WHERE `user_ip` LIKE '$user_ip'";
			$create=mysql_query($create_list) or die (mysql_error());
		}
}
function show_points($id,$db)
{
	change_db($db);
	$select_acc = "SELECT `points` FROM `voting_points` WHERE `id` LIKE '$id'";
	$results=mysql_query($select_acc) or die (mysql_error());
	$row=mysql_fetch_array($results);
	$acc_points=$row['points'];
	return $acc_points;
}
function show_passed_time($db)
{
	change_db($db);
	$user_ip = getenv("REMOTE_ADDR");
	$select_user = "SELECT * FROM `voting` WHERE `user_ip` LIKE '$user_ip'";
	$results=mysql_query($select_user) or die (mysql_error());
	$row=mysql_fetch_array($results);
	$user_time=$row['time'];
	if ($user_time == 0)
		return 0;
	else
	{
		$today=time();
		$passed = $today - $user_time;
		$passed_seconds = $passed %60;
		$passed_minutes_in_seconds = ($passed - $passed_seconds)%3600;
		$passed_minutes = $passed_minutes_in_seconds/60;
		$passed_hours = ($passed - ($passed_seconds + $passed_minutes_in_seconds))/3600;
		$user_passed_time = $passed_hours.":".$passed_minutes.":".$passed_seconds;
		return $user_passed_time;
	}
}
function return_voted_sites($db)
{
	change_db($db);
	$user_ip = getenv("REMOTE_ADDR");
	$check_voted_sites = "SELECT `sites` FROM `voting` WHERE `user_ip` LIKE '$user_ip'";
	$check=mysql_query($check_voted_sites) or die (mysql_error());
	$row=mysql_fetch_array($check);
	$sites=$row['sites'];
	return $sites;
}
function vote($site,$id,$db)
{
	global $tab_sites;
	if(array_key_exists($site, $tab_sites))
	{
		if(!($site & return_voted_sites($db)))
		{
			$user_ip = getenv("REMOTE_ADDR");
			echo "<script language='javascript'>setTimeout(window.open(\"".$tab_sites[$site][1]."\", \"_self\", \"\"),0);</script>";
			$today=time();
			change_db($db);
			$vote_user="UPDATE `voting` SET `sites`=(`sites` + $site), `time`='$today' WHERE `user_ip` LIKE '$user_ip'";
			$results=mysql_query($vote_user) or die(mysql_error());
			$vote_acc="UPDATE `voting_points` SET `points`=(`points` + 1), `date_points`=(`date_points` + 1) WHERE `id` = '$id'";
			$results=mysql_query($vote_acc) or die(mysql_error());
		}
	}
}
function count_tab_sites()
{
	global $tab_sites;
	$counter=0;
	foreach($tab_sites as $key => $value)
		$counter+=$key;
	return $counter;
}
function show_sites_menu($db)
{
	global $tab_sites;
	$user_ip = getenv("REMOTE_ADDR");
	change_db($db);
	foreach($tab_sites as $key => $value)
	{
		$vote_select="SELECT `sites` FROM `voting` WHERE `user_ip` LIKE '$user_ip'";
		$results=mysql_query($vote_select) or die(mysql_error());
		$row=mysql_fetch_array($results);
		$sites=$row['sites'];
		if (!($sites & $key))
			echo "<option value='".$key."'>".$value[0]."</option>";
	}
}
function show_time_to_vote($db)
{
	change_db($db);
	$user_ip = getenv("REMOTE_ADDR");
	$select_user = "SELECT * FROM `voting` WHERE `user_ip` LIKE '$user_ip'";
	$results=mysql_query($select_user) or die (mysql_error());
	$row=mysql_fetch_array($results);
	$user_time=$row['time'];
	$today=time();
	$remaining=43200-($today - $user_time);
	$remaining_seconds = $remaining%60;
	$remaining_minutes_in_seconds = ($remaining - $remaining_seconds)%3600;
	$remaining_minutes = $remaining_minutes_in_seconds/60;
	$remaining_hours = ($remaining - ($remaining_seconds + $remaining_minutes_in_seconds))/3600;
	$user_remaining_time = $remaining_hours.":".$remaining_minutes.":".$remaining_seconds;
	return $user_remaining_time;
}
function show_chars_menu($id,$characters_db)
{
	change_db($characters_db);
	$characters_select="SELECT `guid`,`name` FROM `characters` WHERE `account` = $id";
	$results=mysql_query($characters_select) or die(mysql_error());
	while ($row=mysql_fetch_array($results))
	{
		$char_guid=$row['guid'];
		$char_name=$row['name'];
		echo "<option value='".$char_guid."'>".$char_name."</option>";
	}
}
function add_char_to_session($guid,$characters_db)
{
	change_db($characters_db);
	$select_char = "SELECT `name` FROM `characters` WHERE `guid` LIKE '$guid'";
	$results=mysql_query($select_char) or die (mysql_error());
	$row=mysql_fetch_array($results);
	$_SESSION['char_name'] = $row['name'];
}
function show_rewards($id,$db)
{
	global $language, $tab_rewards;
	change_db($db);
	echo "<table border='1' cellspacing='1' cellpadding='3' align='center'>
	<tr>
		<th colspan='3'>".$language['available_rewards']."</th>
	</tr>";
	$points = show_points($id,$db);
	foreach($tab_rewards as $key => $value)
	{
		$disabled="";
		if($value[1] > $points)
			$disabled=" disabled";
		echo "<form action='index.php' method='post'><input type='hidden' name='reward' value='".$key."'>
		<tr><td>".$value[0]."</td>
		<td>".$value[1]." ".$language['points_2']."</td>
		<td><input type='submit' name='submit' value='".$language['choose']."'".$disabled." ></td></tr></form>";
	}
	echo "</table>";
}
function chose_reward($id,$char_name,$reward,$db,$user,$pass,$ip,$port)
{
	$telnet = @fsockopen($ip, $port, $errno, $errstr, 3);
	if($telnet)
	{
		global $language, $tab_rewards;
		change_db($db);
		$points = show_points($id,$db);
		fgets($telnet,1024);
		$send1 = fputs($telnet, "".$user."\n");
		sleep(3);
		$send2 = fputs($telnet, "".$pass."\n");
		sleep(3);
		if(($send1) && ($send2) && (fgets($telnet,1024) <> ""))
		{
			if(array_key_exists($reward, $tab_rewards))
			{
				if($points >= $tab_rewards[$reward][1])
				{
					// check if it is money
					if ($reward[0] == 'm')
						$send3 = fputs($telnet, "send money ".$char_name." \"".$language['mail_subject']."\" \"".$language['mail_message']."\" ".$tab_rewards[$reward][2]."\n");
					else
						$send3 = fputs($telnet, "send items ".$char_name." \"".$language['mail_subject']."\" \"".$language['mail_message']."\" ".$reward.":".$tab_rewards[$reward][2]."\n");
					sleep(5);
					if($send3)
					{
						$reward_acc="UPDATE `voting_points` SET `points`=(`points` - ".$tab_rewards[$reward][1].") WHERE `id` = '$id'";
						$results=mysql_query($reward_acc) or die(mysql_error());
						echo "<center>".$language['was_given']." ".$tab_rewards[$reward][0]." ".$language['to']." ".$char_name."</center>";
					}
				}
			}
		}
		fclose($telnet);
	}
	else
		echo "<center>".$errstr."</center>";
}
function check_date_points($id,$db)
{
	global $tab_sites;
	change_db($db);
	$check_date_and_datepoints = "SELECT * FROM `voting_points` WHERE `id` = $id";
	$check=mysql_query($check_date_and_datepoints) or die (mysql_error());
	$row=mysql_fetch_array($check);
	$date = $row['date'];
	$date_points = $row['date_points'];
	$today = date("Ymd");
	if($date <> $today)
	{
		$reset_date_and_datepoints = "UPDATE `voting_points` SET `date`='$today', `date_points`='0' WHERE `id` = $id";
		$reset=mysql_query($reset_date_and_datepoints) or die (mysql_error());
		return 1;
	}
	else
		if($date_points >= sizeof($tab_sites))
			return 0;
		else
			return 1;
}
?>